package com.settlement.system.service.impl;

import com.settlement.system.model.entity.OdsOmsRetailReturnOrderDetails;
import com.settlement.system.mapper.OdsOmsRetailReturnOrderDetailsMapper;
import com.settlement.system.service.OdsOmsRetailReturnOrderDetailsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.OdsOmsRetailReturnOrderDetailsQuery;
import com.settlement.system.model.form.OdsOmsRetailReturnOrderDetailsForm;
import com.settlement.system.model.vo.OdsOmsRetailReturnOrderDetailsImportVo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import com.settlement.system.common.util.StringUtils;

/**
 * <p>
 * 退单明细 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-08-02
 */
@Service
public class OdsOmsRetailReturnOrderDetailsServiceImpl extends ServiceImpl<OdsOmsRetailReturnOrderDetailsMapper, OdsOmsRetailReturnOrderDetails> implements OdsOmsRetailReturnOrderDetailsService {

    @Override
    public Page<OdsOmsRetailReturnOrderDetails> getPage(OdsOmsRetailReturnOrderDetailsQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<OdsOmsRetailReturnOrderDetails> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

    @Override
    public List<OdsOmsRetailReturnOrderDetails> getList(OdsOmsRetailReturnOrderDetails odsOmsRetailReturnOrderDetails) {
        return this.baseMapper.getList(odsOmsRetailReturnOrderDetails);
    }

    @Override
     public boolean saveOdsOmsRetailReturnOrderDetails(OdsOmsRetailReturnOrderDetailsForm odsOmsRetailReturnOrderDetailsForm) {
         OdsOmsRetailReturnOrderDetails odsOmsRetailReturnOrderDetails = new OdsOmsRetailReturnOrderDetails();
         BeanUtils.copyProperties(odsOmsRetailReturnOrderDetailsForm, odsOmsRetailReturnOrderDetails);
         return this.saveOrUpdate(odsOmsRetailReturnOrderDetails);
     }

     @Override
     public OdsOmsRetailReturnOrderDetailsForm getOdsOmsRetailReturnOrderDetailsFormData(Long id) {
         OdsOmsRetailReturnOrderDetails odsOmsRetailReturnOrderDetails = this.getById(id);
         OdsOmsRetailReturnOrderDetailsForm odsOmsRetailReturnOrderDetailsForm = new OdsOmsRetailReturnOrderDetailsForm();
         BeanUtils.copyProperties(odsOmsRetailReturnOrderDetails, odsOmsRetailReturnOrderDetailsForm);
         return odsOmsRetailReturnOrderDetailsForm;
     }

     @Override
     public boolean deleteOdsOmsRetailReturnOrderDetails(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<OdsOmsRetailReturnOrderDetailsImportVo> voList)  {
        List<OdsOmsRetailReturnOrderDetails> odsOmsRetailReturnOrderDetailsList = new ArrayList<>();
        for (OdsOmsRetailReturnOrderDetailsImportVo importVo : voList) {
            OdsOmsRetailReturnOrderDetails odsOmsRetailReturnOrderDetails = new OdsOmsRetailReturnOrderDetails();
            BeanUtils.copyProperties(importVo, odsOmsRetailReturnOrderDetails);
            odsOmsRetailReturnOrderDetailsList.add(odsOmsRetailReturnOrderDetails);
        }
        this.saveOrUpdateBatch(odsOmsRetailReturnOrderDetailsList);
    }

    @Override
    public List<OdsOmsRetailReturnOrderDetailsImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<OdsOmsRetailReturnOrderDetails> odsOmsRetailReturnOrderDetailsList = this.getBaseMapper().selectBatchIds(ids);
        List<OdsOmsRetailReturnOrderDetailsImportVo> voList = new ArrayList<>();
        for (OdsOmsRetailReturnOrderDetails odsOmsRetailReturnOrderDetails : odsOmsRetailReturnOrderDetailsList) {
            OdsOmsRetailReturnOrderDetailsImportVo vo = new OdsOmsRetailReturnOrderDetailsImportVo();
            BeanUtils.copyProperties(odsOmsRetailReturnOrderDetails, vo);
            voList.add(vo);
        }
        return voList;
    }
}
